Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set a MockSession context for the initial execution of express code #1038

Closed
wants to merge 2 commits into from

Conversation

cpsievert
Copy link
Collaborator

@cpsievert cpsievert commented Jan 19, 2024

This is currently a quick and dirty draft at addressing #1034.

The main idea here is that if you try to access session attributes on the 1st run of an express app, you get an informative error (to wrap that code in if session: ), so that something like this can work without error:

from shiny import reactive
from shiny.express import session, ui

@reactive.effect
async def _():
    if session:
        x = {"message": "Hello from Python!"}
        await session.send_custom_message("send_alert", x)

ui.tags.script(
    """
    Shiny.addCustomMessageHandler("send_alert", function(x) {
        document.body.innerHTML = x.message;
    });
    """
)

Also, more controversially, require_active_session() will now throw a silent exception when there is no actual session inside a reactive context, so that something like this just works without error:

from shiny import reactive, session
from shiny.express import ui

ui.input_slider("n", "N", 1, 100, 50)

@reactive.effect
def _():
    ui.update_slider("n", value=10)

@cpsievert cpsievert force-pushed the express-mock-session branch from 81d7d08 to 8254fac Compare January 20, 2024 00:00
@wch wch added this to the v0.7.0 milestone Jan 22, 2024
@wch
Copy link
Collaborator

wch commented Jan 23, 2024

Closing because we had a different fix in #1049.

@wch wch closed this Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants